package com.zwq.repository;

import com.zwq.pojo.entity.Machine;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;

public interface MachineRepository extends JpaRepository<Machine, Long> {

    @Query("SELECT m.type, COUNT(m) FROM Machine m GROUP BY m.type")
    List<Object[]> countByType();

    @Query("SELECT m.status, COUNT(m) FROM Machine m GROUP BY m.status")
    List<Object[]> countByStatus();

    @Query("SELECT DATE(m.registerTime), SUM(m.usageCount) FROM Machine m " +
            "WHERE m.registerTime BETWEEN :start AND :end GROUP BY DATE(m.registerTime)")
    List<Object[]> sumUsageCountByDate(LocalDateTime start, LocalDateTime end);

    @Query("SELECT m.name, COUNT(m) FROM Machine m WHERE m.status = 'FAULT' GROUP BY m.name")
    List<Object[]> countFailuresByName();

    List<Machine> findByOwnerId(Integer ownerId);
    List<Machine> findByNumberContaining(String number);
}